package zym.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.SimpleFormatter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.DeptDAO;
import dao.EmpDAO;
import dao.impl.DeptDAOImpl;
import dao.impl.DutyDAOImpl;
import dao.impl.EmpDAOImpl;

import model.Dept;
import model.Duty;
import model.Emp;

import util.Page;
import zym.service.TansferMag;
public class serchTransfer extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		int pageNo=1;
		int pageSize=5;
		Page page=null;
		Emp emp=new Emp();
		EmpDAO empDAO=new EmpDAOImpl();
		Dept dept=new Dept();
		DeptDAO deptDAO=new DeptDAOImpl();
		Duty duty=new Duty();
		TansferMag tf = new TansferMag();
		try{
			if(request.getParameter("pageNo")!=null)
			{
				pageNo=Integer.parseInt(request.getParameter("pageNo"));
			}
			if(request.getParameter("pageSize")!=null)
			{
				pageSize=Integer.parseInt(request.getParameter("pageSize"));
				request.getSession().setAttribute("pageSize", pageSize);
			}
			if(request.getSession().getAttribute("pageSize")!=null)
			{
				pageSize=(Integer) request.getSession().getAttribute("pageSize");
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		
		if("empno".equals(request.getParameter("query_t"))){
			String txt=request.getParameter("querytype");
			int id=empDAO.getIDByEmpno(txt);
			emp=empDAO.getEmpById(id);
			page=tf.getTransferByEmpno(pageNo, pageSize, emp);
		}else if("b_dept".equals(request.getParameter("query_t"))){
			String txt=request.getParameter("querytype");
			dept=deptDAO.getDeptByName(txt);
			page=tf.getTransferByDept(pageNo, pageSize, dept);
		}else if("b_duty".equals(request.getParameter("query_t"))){
			String txt=request.getParameter("querytype");
			duty=new DutyDAOImpl().getDutyByName(txt);
			page=tf.getTransferByDuty(pageNo, pageSize, duty);
		}else if("time".equals(request.getParameter("query_t"))){
			try {
				Date starttime=new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("bdate"));
				Date endtime=new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("edate"));
				page=tf.getTransferByDate(pageNo, pageSize, starttime, endtime);
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else
		{
			page=tf.getAll(pageNo, pageSize);
		}
		
		
		request.setAttribute("page", page);
		
		request.getRequestDispatcher("/Transfer/show_transfer.jsp").forward(request, response);
	}

}
